<template>
  <div>
    <el-row :gutter="8" style="height: 10%">
      <el-col :span="24" style="height: 100%">
        <div class="header">
          <span style="margin-left: 20px">统计月份：</span>
          <el-date-picker
            style="margin-left: 20px"
            v-model="monthRange"
            type="monthrange"
            range-separator="至"
            start-placeholder="开始月份"
            end-placeholder="结束月份"
            value-format="yyyy-MM-dd HH:mm:ss"
          ></el-date-picker>
          <el-button
            type="primary"
            style="margin-left: 20px"
            size="small"
            @click="getPageData"
          >
            查询报表
          </el-button>
        </div>
      </el-col>
    </el-row>
    <el-row :gutter="8" style="height: 80%; margin-top: 20px">
      <el-col :span="24" style="height: 100%">
        <div ref="chart1" style="width: 100%; height: 90%"></div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { getHomePage } from '@/api/finance/home'
export default {
  data: function () {
    return {
      monthRange: null,
      pageData: null,
    }
  },
  methods: {
    getPageData() {
      getHomePage({
        startMonth: this.monthRange
          ? this.monthRange[0]
            ? this.monthRange[0]
            : null
          : null,
        endMonth: this.monthRange
          ? this.monthRange[1]
            ? this.monthRange[1]
            : null
          : null,
      }).then(({ data }) => {
        this.pageData = data
        this.showChart1()
      })
    },
    showChart1() {
      var xAxisDatas = this.pageData.monthDatas.map((x) => x.monthDesc)
      var seriesData1 = this.pageData.monthDatas.map((x) => x.billSum)
      var seriesData2 = this.pageData.monthDatas.map((x) => x.billReturnSum)
      var seriesData3 = this.pageData.monthDatas.map((x) => x.returnSum)
      var seriesData4 = this.pageData.monthDatas.map((x) => x.bills.length)
      var seriesData5 = this.pageData.monthDatas.map(
        (x) => x.returnedBills.length
      )
      var seriesData6 = this.pageData.monthDatas.map(
        (x) => x.partReturnBills.length
      )
      var seriesData7 = this.pageData.monthDatas.map(
        (x) => x.unreturnBills.length
      )
      const chart1 = this.$refs.chart1
      if (chart1) {
        const myChart = this.$echarts.init(chart1)
        const option = {
          title: {
            text: '财务报表',
            textStyle: { fontSize: 20 },
            left: '40%',
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: { type: 'cross' },
          },
          legend: { align: 'left', right: '10%' },
          xAxis: {
            data: xAxisDatas,
          },
          yAxis: [
            {
              type: 'value',
              name: '金额',
              axisLabel: {
                formatter: '{value} 元',
              },
            },
            {
              type: 'value',
              name: '笔数',
              position: 'right',
              axisLabel: {
                formatter: '{value} 笔',
              },
              splitLine: {
                show: false,
              },
            },
          ],
          series: [
            {
              name: '开票金额',
              type: 'bar',
              yAxisIndex: 0,
              data: seriesData1,
            },
            {
              name: '开票回款金额',
              type: 'bar',
              yAxisIndex: 0,
              data: seriesData2,
            },
            {
              name: '当月回款金额',
              type: 'bar',
              yAxisIndex: 0,
              data: seriesData3,
            },
            {
              name: '开票笔数',
              type: 'line',
              yAxisIndex: 1,
              data: seriesData4,
            },
            {
              name: '已到账笔数',
              type: 'line',
              yAxisIndex: 1,
              data: seriesData5,
            },
            {
              name: '部分到账笔数',
              type: 'line',
              yAxisIndex: 1,
              data: seriesData6,
            },
            {
              name: '未到账笔数',
              type: 'line',
              yAxisIndex: 1,
              data: seriesData7,
            },
          ],
        }
        myChart.setOption(option)
        window.addEventListener('resize', function () {
          myChart.resize()
        })
      }
      this.$on('hook:destroyed', () => {
        window.removeEventListener('resize', function () {
          myChart.resize()
        })
      })
    },
  },
  mounted: function () {
    this.getPageData()
  },
}
</script>
<style scoped>
.header {
  width: 100%;
  padding-top: 10px;
  height: auto;
  font-size: 14px;
  font-family: PingFangSC-Regular, PingFang SC;
  font-weight: 400;
  background: #ffffff;
  border-radius: 5px;
  display: flex;
  margin-bottom: 11px;
  align-items: center;
}
</style>
